//
//  WFLoginViewController.m
//  WisdomFarm
//
//  Created by 心情 on 2017/10/26.
//  Copyright © 2017年 sky. All rights reserved.
//

#import <BlocksKit/UIControl+BlocksKit.h>

#import "WFLoginViewController.h"
#import "UIButton+WF.h"
#import "UINavigationController+WFUtils.h"
#import "WFRegisterViewController.h"
#import "WFLoginPhoneCell.h"
#import "WFLoginPasswordCell.h"

#import "WFLoginRequest.h"
#import "WFPasswordResetViewController.h"

@interface WFLoginFooterView : UIView

@property (nonatomic, strong) UIButton *rememberBtn;
@property (nonatomic, strong) UIButton *forgetBtn;
@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) UIButton *registerBtn;

@end

@implementation WFLoginFooterView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _rememberBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_rememberBtn];
        [_rememberBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(12);
            make.top.offset(22);
        }];
        _rememberBtn.titleLabel.font = SCFont(13);
        [_rememberBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_rememberBtn setTitle:@"记住密码" forState:UIControlStateNormal];
        [_rememberBtn setImage:[UIImage imageNamed:@"login_pitchr_no"] forState:UIControlStateNormal];
        [_rememberBtn setImage:[UIImage imageNamed:@"login_pitchr"] forState:UIControlStateSelected];
        
        _forgetBtn = [[UIButton alloc] init];
        [self addSubview:_forgetBtn];
        [_forgetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-12);
            make.top.offset(22);
        }];
        _forgetBtn.titleLabel.font = SCFont(13);
        [_forgetBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_forgetBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];

        _loginBtn = [UIButton mainButtonWithTitle:@"登录"];
        [self addSubview:_loginBtn];
        [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.rememberBtn.mas_bottom).offset(45);
            make.left.right.insets(UIEdgeInsetsMake(0, 12, 0, 12));
            make.height.mas_equalTo(48);
        }];
        
        _registerBtn = [UIButton confirmButtonWithTitle:@"注册"];
        [self addSubview:_registerBtn];
        [_registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.loginBtn.mas_bottom).offset(12);
            make.left.right.insets(UIEdgeInsetsMake(0, 12, 0, 12));
            make.height.mas_equalTo(48);
        }];
    }
    return self;
}

@end

@interface WFLoginViewController ()

@property (nonatomic, strong) NSArray<UITableViewCell *> *cells;

@property (nonatomic, strong) WFLoginPhoneCell *nameCell;
@property (nonatomic, strong) WFLoginPasswordCell *passwordCell;

@property (nonatomic, strong) WFLoginFooterView *footerView;


@end

@implementation WFLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    @weakify(self);
    self.title = @"登录";
    UIImageView *bg = [[UIImageView alloc] init];
    bg.image = [UIImage imageNamed:@"login_bg"];
    self.tableView.backgroundView = bg;
    self.tableView.contentInset = UIEdgeInsetsMake(65, 0, 0, 0);
    self.tableView.allowsSelection = NO;
    self.tableView.rowHeight = 63;
    
    self.navigationItem.leftBarButtonItem = [self.navigationController wf_backItemWithHandle:^(id sender) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [self.tableView registerClass:[WFLoginPhoneCell class] forCellReuseIdentifier:@"WFLoginPhoneCell"];
    [self.tableView registerClass:[WFLoginPasswordCell class] forCellReuseIdentifier:@"WFLoginPasswordCell"];

    self.nameCell = [WFLoginPhoneCell new];
    self.passwordCell = [WFLoginPasswordCell new];
    
    self.footerView = [WFLoginFooterView new];
    self.footerView.height = 187;
    self.tableView.tableFooterView = self.footerView;
    
    [self.footerView.registerBtn bk_addEventHandler:^(id sender) {
        @strongify(self);
        WFRegisterViewController *registerVC = [WFRegisterViewController new];
        [self.navigationController pushViewController:registerVC animated:YES];
    } forControlEvents:UIControlEventTouchUpInside];
    
    [self.footerView.loginBtn bk_addEventHandler:^(id sender) {
        @strongify(self)
        [self onLogin];
    } forControlEvents:UIControlEventTouchUpInside];
    
    [self.footerView.forgetBtn addTarget:self action:@selector(onForget) forControlEvents:UIControlEventTouchUpInside];
    self.footerView.rememberBtn.hidden = YES;
    self.footerView.forgetBtn.hidden = YES;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return self.nameCell;
    }
    return self.passwordCell;
}

//MARK:- event
- (void)onLogin {
    if (self.nameCell.textField.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入手机号码"];
        return;
    }
    if (self.passwordCell.textField.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入密码"];
        return;
    }
    WFLoginRequest *request = [WFLoginRequest new];
    request.phone = self.nameCell.textField.text;
    request.password = [self.passwordCell.textField.text MD5];
    [SVProgressHUD show];
    [request startWithBlock:^(__kindof MZBaseRequest *request, NSError *error) {
        if (error) {
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
            return;
        }
        [SVProgressHUD showSuccessWithStatus:@"登录成功!"];
        if (self.loginFinished) {
            self.loginFinished(YES);
        }
    }];
}

- (void)onForget {
    WFPasswordResetViewController *controller = [WFPasswordResetViewController new];
    [self.navigationController pushViewController:controller animated:YES];
}

@end
